<template>
  <div class="container">

    <div class="header">
      <img src="../assets/topleft.png"/>
    </div>

    <div class='body' :style="`height: ${this.height - 1}px;`">

      <img src="../assets/bg.png" width="100%" height="100%"/>

      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <span class="title">欢迎回家</span>
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
            <!--<svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>-->
            <img src="../assets/person.png" slot="prefix" style="width:16px;position: relative;left: 2px;top: 6px;"/>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
          >
            <img src="../assets/password.png" slot="prefix" style="width:16px;position: relative;left: 2px;top: 6px;"/>
            <!--<svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>-->
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="!needMessageCode">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode"/>
          </div>
        </el-form-item>

        <el-form-item prop="validateCode" v-else>
          <el-input
            v-model="loginForm.validateCode"
            auto-complete="off"
            placeholder="短信验证码"
            style="width: 63%"
            @keyup.enter.native="handleLogin">
          </el-input>
          <div class="login-code">
            <el-button
              size="medium"
              type="primary"
              :disabled="!codeButtonInfo.canClick"
              @click="handleGetMessageCode">
              {{codeButtonInfo.text}}
            </el-button>
          </div>
        </el-form-item>

        <!-- <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>-->
        <el-form-item style="width:100%;">
          <el-button
            :loading="loading"
            size="medium"
            :style="{background:'#B14C5C',border:'none !important'}"
            type="danger"
            style="width:100%;"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>

      <!--  底部  -->
      <div class="el-login-footer">
        <!--<span>Copyright © 2018-2019 ruoyi.vip All Rights Reserved.</span>-->

        <div class="banner">
          <span>首页&nbsp;|&nbsp;</span>
          <span>人事管理&nbsp;|&nbsp;</span>
          <span>薪资管理&nbsp;|&nbsp;</span>
          <span>流程管理&nbsp;|&nbsp;</span>
          <span>系统管理&nbsp;</span>
        </div>

        <div class="banner">
          <span>邮箱：marketing@qihangedu.com</span>
        </div>
        <div class="banner">
          <span>地址:上海市纪念路8号财大科技园2栋</span>
        </div>

      </div>

    </div>


  </div>

</template>

<script>
  import { debounce } from 'lodash'
  import getMessageCode from '@/mixins/getCode'
  import { getCodeImg } from '@/api/login'
  import Cookies from 'js-cookie'
  import { encrypt, decrypt } from '@/utils/jsencrypt'

  export default {
    name: 'Login',
    mixins: [getMessageCode],
    data() {
      return {
      	height:window.innerHeight,
        codeUrl: '',
        cookiePassword: '',
        loginForm: {
          username: '',
          password: '',
          rememberMe: false,
          code: '',
          validateCode: '', // 短信验证码
          uuid: ''
        },
        loginRules: {
          username: [
            { required: true, trigger: 'blur', message: '用户名不能为空' }
          ],
          password: [
            { required: true, trigger: 'blur', message: '密码不能为空' }
          ],
          code: [{ required: true, trigger: 'change', message: '验证码不能为空' }],
          validateCode: [{ required: true, trigger: 'blur', message: '短信验证码不能为空' }]
        },
        loading: false,
        redirect: undefined,
        currentPhone: undefined,
        needMessageCode: false // 是否需要验证短信验证码
      }
    },
    watch: {
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect
        },
        immediate: true
      }
    },
    created() {
      this.getCode()
      this.getCookie()
      let that = this;
      window.onresize = function() {
      	that.height = window.innerHeight
      }

    },
    methods: {
      getCode() {
        getCodeImg().then(res => {
          this.codeUrl = 'data:image/gif;base64,' + res.img
          this.loginForm.uuid = res.uuid
        })
      },
      getCookie() {
        const username = Cookies.get('username')
        const password = Cookies.get('password')
        const rememberMe = Cookies.get('rememberMe')
        this.loginForm = {
          username: username === undefined ? this.loginForm.username : username,
          password: password === undefined ? this.loginForm.password : decrypt(password),
          rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
        }
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            if (this.loginForm.rememberMe) {
              Cookies.set('username', this.loginForm.username, { expires: 30 })
              Cookies.set('password', encrypt(this.loginForm.password), { expires: 30 })
              Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
            } else {
              Cookies.remove('username')
              Cookies.remove('password')
              Cookies.remove('rememberMe')
            }
            this.$store.dispatch('Login', this.loginForm)
              .then((result) => {
                if (+result.code === 200) {
                  // this.$router.push({ path: this.redirect || "/" });
                  this.$router.push({ path: '/' })
                } else {
                  if (+result.code === -1) {
                    // 需要验证短信验证码
                    this.needMessageCode = true
                    this.currentPhone = result.phone
                  }
                  this.getCode()
                  this.$notify.error({
                    title: '错误',
                    message: result.msg || '登录失败！'
                  })
                }
              })
              .catch((error) => {
                // console.log(error)
                this.getCode()
                this.$notify.error({
                  title: '错误',
                  message: error.message || '登录失败!'
                })
              })
              .finally(() => {
                this.loading = false
              })
          }
        })
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss">


  .container {
    .header {
      height: 66px;
	    width: 100%;
	    background: #fff;
	    position: absolute;
	    top: 0;
	    z-index: 999;
	    left: 0;
      img {
        width: 200px;
        position: absolute;
        left: 10%;
      }

    }

    .body {
      position: relative;
      /*height: 680px;*/
      overflow: hidden;
    }
  }


  .title {
    margin: 6px auto 20px auto;
    display: block;
    text-align: left;
    color: #B14C5C;
    font-size: 20px;
    letter-spacing: 2px;
  }
  
  /*添加手机或者移动设备的适配*/
  @media only screen and (max-width: 600px) {
  	.login-form {
		    position: absolute;
		    left: 50% !important;
		    margin-left: -180px !important;
		    top: 120px;
		    border-radius: 6px;
		    background: #ffffff;
		    width: 360px;
		    background: rgba(255,255,255,0.76);
		    padding: 25px 25px 5px 25px;
		}
  }

  .login-form {
    position: absolute;
    left: 66%;
    top: 170px;
    border-radius: 6px;
    background: #ffffff;
    width: 360px;
    background: rgba(255, 255, 255, 0.76);
    padding: 25px 25px 5px 25px;

    .el-input {
      height: 38px;

      input {
        height: 38px;
      }
    }

    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }

  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }

  .login-code {
    width: 33%;
    height: 38px;
    float: right;

    img {
      cursor: pointer;
      vertical-align: middle;
      width: 100%;
    }
  }

  .el-login-footer {
    height: 82px;
    line-height: 40px;
    position: absolute;
    bottom: 0px;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
    background-color: #B14C5C;
    opacity: 0.8;

    .banner {
      margin-bottom: -18px;
    }
  }

  .login-code {
    .el-button--primary {
      background-color: #B14C5C;
      border-color: #B14C5C;
      width: 100%;
      padding-left: 0;
      padding-right: 0;
      text-align: center;

      &.is-disabled,
      &:hover,
      &:active {
        background-color: #B16A6F;
        border-color: #B16A6F;
      }
    }
  }
</style>
